﻿using System;
using System.Windows;
using Microsoft.Win32;

namespace DreamLand.UI.MagicBook
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application, IDisposable
    {
        #region Fields

        /// <summary>
        /// Variable to indicate that dispose is called.
        /// </summary>
        private bool disposed;

        #endregion

        #region Constructor

        /// <summary>
        /// Initializes a new instance of the App class.
        /// </summary>
        internal App()
        {
            this.Startup += this.App_Startup;
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets a value indicating whether whether the task bar needs to be hidden.
        /// </summary>
        internal static bool HideTaskBar { get; set; }

        #endregion

        /// <summary>
        /// The Dispose method allows the caller to explicitly release resources on demand, earlier 
        /// than the resources would be released if left to the garbage collector.
        /// </summary>                
        public void Dispose()
        {
            this.Dispose(true);
            GC.SuppressFinalize(this);
        }

        #region IDisposable Members

        /// <summary>
        /// The Dispose method helps to release the resources.
        /// </summary>
        /// <param name="disposing">variable indicating if its an explicit call or implicit call</param>        
        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {
                }

                SystemEvents.SessionSwitch -= this.SystemEvents_SessionSwitch;
                this.disposed = true;
            }
        }

        #endregion

        /// <summary>
        /// Called when the application starts.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The event args. </param>
        private void App_Startup(object sender, StartupEventArgs e)
        {
            SystemEvents.SessionSwitch += this.SystemEvents_SessionSwitch;
        }

        /// <summary>
        /// Handles the SessionSwitch event.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The event args.</param>
        private void SystemEvents_SessionSwitch(object sender, SessionSwitchEventArgs e)
        {
            if (e.Reason == SessionSwitchReason.SessionUnlock)
            {
                HideTaskBar = true;
            }
        }     
    }
}
